// 定义环境变量前缀
const project_name = 'XLSD';

const total_config = {
    port: 3000,
    wx: get_sys_config(),
    database: get_mysql_config(),
    minio: get_minio_config()
};

// 获取小程序配置
function get_sys_config() {
    let appid = process.env[`${project_name}_WX_APPID`];    // 小程序 appid
    let secret = process.env[`${project_name}_WX_SECRET`];  // 小程序 secret
    if (!appid || !secret) {
        console.log('读取不到系统环境变量:');
        console.log(`    - ${project_name}_WX_APPID`);
        console.log(`    - ${project_name}_WX_SECRET`);
        process.exit(1);
    }
    return { appid, secret };
}

// 获取mysql配置
function get_mysql_config() {
    let host = process.env[`${project_name}_MYSQL_HOST`];   // 数据库地址
    let port = process.env[`${project_name}_MYSQL_PORT`];   // 数据库端口
    let user = process.env[`${project_name}_MYSQL_USER`];   // 数据库用户名
    let pswd = process.env[`${project_name}_MYSQL_PSWD`];   // 数据库密码
    let database = process.env[`${project_name}_MYSQL_DB`]; // 数据库名称
    if (!host || !port || !user || !pswd || !database) {
        console.log('读取不到系统环境变量:');
        console.log(`    - ${project_name}_MYSQL_HOST`);
        console.log(`    - ${project_name}_MYSQL_PORT`);
        console.log(`    - ${project_name}_MYSQL_USER`);
        console.log(`    - ${project_name}_MYSQL_PSWD`);
        console.log(`    - ${project_name}_MYSQL_DB`);
        process.exit(1);
    }
    return { host, port, user, pswd, database };
}

// 获取MinIO配置
function get_minio_config() {
    let endPoint = process.env[`${project_name}_MINIO_ENDPOINT`];   // minio ip
    let port = process.env[`${project_name}_MINIO_PORT`];           // minio 端口
    let useSSL = process.env[`${project_name}_MINIO_USESSL`];       // minio 是否使用 SSL
    let accessKey = process.env[`${project_name}_MINIO_AKEY`];      // minio 访问密钥
    let secretKey = process.env[`${project_name}_MINIO_SKEY`];      // minio 密钥
    if (!endPoint || !port || !useSSL || !accessKey || !secretKey) {
        console.log('读取不到系统环境变量:');
        console.log(`    - ${project_name}_MINIO_ENDPOINT`);
        console.log(`    - ${project_name}_MINIO_PORT`);
        console.log(`    - ${project_name}_MINIO_USESSL`);
        console.log(`    - ${project_name}_MINIO_AKEY`);
        console.log(`    - ${project_name}_MINIO_SKEY`);
        process.exit(1);
    }
    return { endPoint, port, useSSL, accessKey, secretKey };
}

module.exports = total_config;